$(function(){
    NProgress.done();
    var url = location.search;
    var arr = url.split('')
    var noteId='';
    for (let i=8;i<arr.length;i++){
        noteId +=arr[i];
    }
    showComments(noteId);
    $('#articleid').val(noteId)
    $.ajax({
        type:"get",
        url:"/cloud_note/read",
        data:{"noteId":noteId},
        dataType:"json",
        success:function(result){
            $('#co').prepend(result.content)
            $('.title').prepend('<h4>'+result.title+'</h4>')
            $('.readcount').html(result.visits)
            $('#ti').html(getYMDHMS(result.pubtime).date)
            $('.day').html(getYMDHMS(result.pubtime).day)
            $('.month').prepend(getYMDHMS(result.pubtime).month)
            $('.year').html(getYMDHMS(result.pubtime).year)
            $('.r-title').html(result.title)
        }
    })
})
function showComments(noteId){
    $.ajax({
        type:"get",
        url:"/cloud_note/showComments",
        data:{"noteId":noteId},
        dataType:"json",
        success:function (result) {
            for (let i = 0; i < result.length; i++) {
                if(result[i].parentId==0){
                    var str = '<li class="zoomIn article">' +
                        '<div class="comment-parent">' +
                        '    <a name="remark-1"></a>' +
                        '    <img src="https://z3.ax1x.com/2021/07/02/R6Rlcj.jpg">' +
                        '    <div class="info">' +
                        '         <span class="username">'+result[i].nickname+'</span>' +
                        '    </div>' +
                        '    <div class="comment-content">' +
                        '        '+result[i].content+'' +
                        '    </div>' +
                        '        <p class="info info-footer">' +
                        '        <span class="comment-time">'+getYMDHMS(result[i].createTime).date1+'</span>' +
                        '        <a href="javascript:;" class="btn-reply" data-targetid="'+result[i].id+'" data-targetname="'+result[i].nickname+'">回复</a>' +
                        '        </p>' +
                        '</div>' +
                        '<hr />';
                    if(result[i].children.length>0){
                        for (let j = 0; j < result[i].children.length; j++) {
                            str +='<div class="comment-child">' +
                                '<a name="reply-1"></a>' +
                                '<img src="https://z3.ax1x.com/2021/07/02/R6Rlcj.jpg">' +
                                '<div class="info">' +
                                '<span class="username">'+result[result[i].children[j]].nickname+'</span>' +
                                '<span style="padding-right:0;margin-left:-5px;">回复</span>' +
                                '<span class="username">'+result[result[i].children[j]].parentName+'</span>' +
                                '<span>'+result[result[i].children[j]].content+'</span>' +
                                '</div>' +
                                '<p class="info">' +
                                '<span class="comment-time">'+getYMDHMS(result[result[i].children[j]].createTime).date1+'</span>' +
                                '<a href="javascript:;" class="btn-reply" data-targetid="'+result[result[i].children[j]].id+'" data-targetname="'+result[result[i].children[j]].nickname+'">回复</a>' +
                                '</p>' +
                                '</div>'
                        }
                    }
                    str += '<div class="replycontainer layui-hide">' +
                        '<input type="text" id="nickname1" placeholder="请起一个昵称" style="margin-bottom: 10px">' +
                        '<input type="hidden" id="parentId" name="targetUserId" value="0">' +
                        '<div class="layui-form-item">' +
                        '<textarea name="replyContent" lay-verify="replyContent" placeholder="请输入回复内容" class="layui-textarea" style="min-height:80px;"></textarea>' +
                        '</div>' +
                        '<div class="layui-form-item">' +
                        '<button class="layui-btn layui-btn-xs" lay-submit="formReply" lay-filter="formReply">提交</button>' +
                        '</div>' +
                        '</div>' +
                        '</li>';
                    $('#blog-comment').append(str)
                }
            }
        }
    })
}
$('.formReply').click(function () {
    alert("666")
})
$('#commentsubmit').click(function saveComment(){
    var noteId = $('#articleid').val();
    var content = $('#remarkEditor').val();
    var nickname = $('#nickname').val();
    var parentId = 0;
    var date = getYMDHMS(new Date().getTime()).date
    if (nickname==""){
        nickname = "未命名"
    }
    $.ajax({
        type:"post",
        url:"/cloud_note/addComment",
        data: {"noteId":noteId,"content":content,"nickname":nickname,"createtime":date,"parentId":parentId},
        dataType: "json",
        success:function (result){
            $('#blog-comment').empty();
            $('#remarkEditor').val("");
            $('#nickname').val("");
            showComments(noteId);
        }
    })
})
function getYMDHMS(a) {
    var time = new Date(a)
    var year = time.getFullYear(),
        month = time.getMonth() + 1,
        day = time.getDate(),
        hours = time.getHours(),
        minute = time.getMinutes(),
        second = time.getSeconds();

    if (month < 10) { month = '0' + month; }
    if (day < 10) { day = '0' + day; }
    if (hours < 10) { hours = '0' + hours; }
    if (minute < 10) { minute = '0' + minute; }
    if (second < 10) { second = '0' + second; }

    return {
        year: year,
        month: month,
        day: day,
        hours: hours,
        minute: minute,
        second: second,
        date: year+"-"+month+"-"+day+" "+hours+':'+minute+':'+second,
        date1: year+"-"+month+"-"+day
    }
}　

